package com.ygk.bluepath.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import utils.DateUtil;
import utils.Inf;
import utils.LogUtils;

public class LBSService extends Service implements LocationListener {

    public static final String TAG = "LBSService";
    // 3000ms --minimum time interval between location updates, in milliseconds
    private static final long minTime = 3000;
    // 最小变更距离 10m --minimum distance between location updates, in meters
    private static final float minDistance = 50;

    String tag = this.toString();
    private LocationManager locationManager;
    //	private LocationListener locationListener;
    private Location location;
    //private GpsStatus gStatus;
    private GpsSatelliteListener gpsSatelliteListener;
    private final IBinder mBinder = new LBSServiceBinder();

    boolean flag;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        flag = true;

        super.onCreate();
        //	HApp.getInstance().tt("手机第一次使用GPS定位等待的时间比较长，请耐心等候……");
        startService();
        Log.i(TAG, "in onCreate method.");
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        stopService();
        super.onDestroy();
        Log.i(TAG, "in onDestroy method.");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return mBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        doJob();// 调用方法启动线程
        return super.onStartCommand(intent, flags, startId);
    }

    public class LBSServiceBinder extends Binder {
        LBSService getService() {
            return LBSService.this;
        }
    }

    public void startService() {

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //	locationListener = new LBSServiceListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                minTime, minDistance, this);
        gpsSatelliteListener = new GpsSatelliteListener();
        locationManager.addGpsStatusListener(gpsSatelliteListener);
        Log.i(TAG, "in startService method.");
    }

    public void stopService() {
        if (locationManager != null && gpsSatelliteListener != null) {
            flag = false;
            locationManager.removeUpdates(this);
            locationManager.removeGpsStatusListener(gpsSatelliteListener);
        }
        Log.i(TAG, "in stopService method.");
    }


    // 方法：
    public void doJob() {
        new Thread() {

            public void run() {

                while (flag) {

                    try {// 睡眠一段时间
                        Thread.sleep(minTime);

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                    Intent intent = new Intent();// 创建Intent对象

                    intent.setAction(Inf.actionLocationGet);

                    // ------------gps定位
                    //LogUtil.e("start");
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    //	gStatus = locationManager.getGpsStatus(null);
                    // 获取默认最大卫星数
                    //	int maxSatellites = gStatus.getMaxSatellites();
                    //	Iterable<GpsSatellite> iterable = gStatus.getSatellites();
                    //	Iterator<GpsSatellite> iterator = iterable.iterator();
                /*	int x = 0;

					while (iterator != null && iterator.hasNext()
							&& x <= maxSatellites) {
						GpsSatellite gpsSatellite = (GpsSatellite) iterator
								.next();
						if (gpsSatellite.usedInFix())
							x++;

					}*/
                    double latitude, longitude;
                    String accuracy;
                    float speed;
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        accuracy = location.getAccuracy() + "";
                        speed = location.getSpeed();
                    } else {
                        latitude = 0.0;
                        longitude = 0.0;
                        accuracy = "未知 ";
                        speed = 0;
                    }

                    Bundle bundle = new Bundle();
                    bundle.putDouble("latitude", latitude);
                    bundle.putDouble("longitude", longitude);
                    bundle.putString("accuracy", accuracy + "m");
                    bundle.putFloat("speed", speed);
                    //	bundle.putString("Satenum", x + "个");

                    bundle.putString("date", DateUtil.getDateString());
                    intent.putExtras(bundle);

                    sendBroadcast(intent);// 发送广播

                }

            }

        }.start();

    }



    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        LogUtils.e("onLocationChanged");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

}
